package org.groupg.practice.math3;

import org.apache.commons.math3.linear.*;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.math3.util.FastMath;

public class CommonsMathDemo {
    public static void main(String[] args) {
        // 1. 线性代数：矩阵乘法
        RealMatrix matrixA = MatrixUtils.createRealMatrix(new double[][]{{1, 2}, {3, 4}});
        RealMatrix matrixB = MatrixUtils.createRealMatrix(new double[][]{{5}, {6}});
        RealMatrix result = matrixA.multiply(matrixB);
        System.out.println("矩阵乘法结果:\n" + result);

        // 2. 解线性方程组
        RealMatrix coefficients = MatrixUtils.createRealMatrix(new double[][]{{2, 3}, {1, -1}});
        RealVector constants = new ArrayRealVector(new double[]{8, 3});
        DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();
        RealVector solution = solver.solve(constants);
        System.out.println("方程解: x=" + solution.getEntry(0) + ", y=" + solution.getEntry(1));

        // 3. 统计计算
        DescriptiveStatistics stats = new DescriptiveStatistics();
        stats.addValue(10);
        stats.addValue(20);
        stats.addValue(30);
        System.out.println("均值: " + stats.getMean());
        System.out.println("标准差: " + stats.getStandardDeviation());

        // 4. 快速数学计算（优化版Math）
        double fastSin = FastMath.sin(FastMath.PI / 2);
        System.out.println("sin(π/2) = " + fastSin); // 输出: 1.0
    }
}